$(function () {
    var aid = "8412";
    var key = "8412";

    //红外设定
    var edge;
    var switch_status;                                  //传感器状态，0为关，1为开
    var myDetectMac = "00:12:4B:00:1A:BE:59:FE";                //传感器的MAC地址
    var detection = new WSNRTConnect(aid, key);        //创建数据连接服务对象
    detection.setServerAddr("192.168.12.110:28080");					//服务器地址
    detection.connect();

    //红外初始化
    detection.onConnect = function () {                                //连接成功回调函数
        detection.sendMessage(myDetectMac, "{A0=?}");           		//向传感器发送数据
        $("#DetectionConnectState").text("红外探测服务连接成功！");
    };

    detection.onConnectLost = function () {                            //数据服务掉线回调函数
        $("#DetectionConnectState").text("红外探测服务掉线！");
    };

    //红外检测回调函数
    detection.onmessageArrive = function (mac, dat) {                  //消息处理回调函数
        if (mac == myDetectMac) {                               //判断传感器Mac地址
            if (dat[0] == '{' && dat[dat.length - 1] == '}') {  //判断字符串首尾是否为{}
                dat = dat.substr(1, dat.length - 2);            //截取{}内的字符串
                var its = dat.split(',');                       //以‘,’来分割字符串
                for (var x in its) {
                    var t = its[x].split('=');                //以‘=’来分割字符串
                    if (t.length != 2) continue;
                    if (t[0] == "A0") {                         //判断参数A0
                        edge = parseInt(t[1]);
                        if (switch_status == 1) {
                            if ((edge & 0x01) == 1) {           //有人时进行的操作
                                $("#detection_state").text("有人");
                                $("#bgd_images").attr("src", "images/alarm-active.gif");
                                $("#imgSnapshot").click();        //截屏
                                if (light_switch == 0) {
                                    $("#light_switch").click();
                                    light_switch = 1;
                                }
                                $("light_state").text("灯已打开");
                            } else {                            //没人时进行的操作
                                $("#detection_state").text("没人");
                                $("#bgd_images").attr("src", "images/alarm-off.png");
                                if (light_switch == 1) {              //警报灯如果打开了就关闭
                                    $("#light_switch").click();
                                    light_switch = 0;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    //红外检测开关
    $("#detection_switch").click(function () {
        if (switch_status) {    //关闭检测模式
            $("#detection_switch").text("打开检测模式");
            $("#detection_state").text("检测关闭");
            $("#bgd_images").attr("src", "images/alarm-off.png");
            if (light_switch == 1) {      //警报灯如果打开了就关闭
                $("#light_switch").click();
                light_switch = 0;
            }
            $("light_state").text("灯已关闭");
            switch_status = 0;
        } else {
            switch_status = 1;
            $("#detection_switch").text("关闭检测模式");
            detection.sendMessage(myDetectMac, "{OD1=0,A0=?}");          //发送打开指令,并发送一次请求查询指令 
        }
    });
});